package com.itcat.controller;

import com.itcat.common.CommonPage;
import com.itcat.common.CommonResult;
import com.itcat.mysql.pojo.UmsResource;
import com.itcat.service.UsmResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/4/3 19:21]
 */
@RestController
@RequestMapping("/resource")
public class UsmResourceContrller {

    @Autowired
    private UsmResourceService usmResourceService;

    /***
     * 添加后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:26
     */
    @PostMapping("create")
    private CommonResult create(@RequestBody UmsResource umsResource) {
        usmResourceService.create(umsResource);
        return CommonResult.success();
    }

    /***
     * 根据ID删除后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:26
     */
    @PostMapping("delete/{id}")
    private CommonResult delete(@PathVariable Long id) {
        usmResourceService.delete(id);
        return CommonResult.success();
    }

    /***
     * 分页模糊查询后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:27
     */
    @GetMapping("list")
    private CommonResult list(@RequestParam(required = false) Long categoryId,
                              @RequestParam(required = false) String nameKeyword,
                              @RequestParam(required = false) String urlKeyword,
                              @RequestParam(required = false) Integer pageNum,
                              @RequestParam(required = false) Integer pageSize) {
        CommonPage<UmsResource> page = usmResourceService.list(categoryId, nameKeyword, urlKeyword, pageNum, pageSize);
        return CommonResult.success(page);
    }

    /***
     * 查询所有后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:27
     */
    @GetMapping("listAll")
    private CommonResult listAll() {
        List<UmsResource> list = usmResourceService.listAll();
        return CommonResult.success(list);
    }

    /***
     * 修改后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:26
     */
    @PostMapping("update/{id}")
    private CommonResult update(@PathVariable Long id, @RequestBody UmsResource umsResource) {
        usmResourceService.update(id, umsResource);
        return CommonResult.success();
    }

    /***
     * 根据ID获取资源详情
     * @author Cr Mr
     * @date 2022/4/3 19:29
     */
    @GetMapping("resource/{id}")
    private CommonResult resource(@PathVariable Long id) {
        UmsResource resource = usmResourceService.resource(id);
        return CommonResult.success(resource);
    }
}
